package com.file.manage.infrastructure.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.file.manage.infrastructure.base.dto.base.BaseDTO;
import com.file.manage.infrastructure.base.entiry.BaseEntity;
import com.google.common.base.Ascii;
import com.google.common.base.CaseFormat;

import java.util.*;

/**
 * @ClassName: BeanConvertUtil
 * @author: hsy
 * @date: 2022/12/5 20:57
 * @description: bean转换工具类
 */
public class ConvertUtil {


    /**
     * 单个bean转换
     *
     * @param source
     * @param t
     * @return
     */
    public static <T> T convert(Object source, Class<T> t) {
        return BeanUtil.copyProperties(source, t);
    }

    /**
     * 批量转换
     *
     * @param source
     * @param t
     * @return
     */
    public static <T> List<T> convertList(Collection<?> source, Class<T> t) {
        List<T> ts = new ArrayList<>();
        if (source != null) {
            for (Object s : source) {
                T t1 = BeanUtil.copyProperties(s, t);
                ts.add(t1);
            }
        }
        return ts;
    }

    /**
     * 获取ids
     *
     * @param source
     * @return
     */
    public static List<String> getIds(List<?> source) {
        List<String> r = new ArrayList<>();
        if (CollUtil.isNotEmpty(source)) {
            for (Object o : source) {
                if (o instanceof BaseDTO) {
                    r.add(((BaseDTO) o).getId());
                } else if (o instanceof BaseEntity) {
                    r.add(((BaseEntity<?>) o).getId());
                }
            }
        }
        return r;
    }

    /**
     * 获取map k：id
     *
     * @param source
     * @return
     */
    public static <T> Map<String, T> getMap(List<T> source) {
        Map<String, T> r = new HashMap<>();
        if (CollUtil.isNotEmpty(source)) {
            for (T o : source) {
                if (o instanceof BaseDTO) {
                    r.put(((BaseDTO) o).getId(), o);
                } else if (o instanceof BaseEntity) {
                    r.put(((BaseEntity<?>) o).getId(), o);
                }
            }
        }
        return r;
    }
}
